package com.qianclass.talklive.app.enter;

import com.qianclass.talklive.app.utils.Constant;
import com.qianclass.talklive.app.utils.Info;
import com.qianclass.talklive.app.utils.LogUtil;
import com.qianclass.talklive.app.utils.VerificationKeyUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by itgoyo on 17-6-14.
 */

public class PlayRecordModelImpl implements  PlayRecordModel {
    @Override
    public void userPlayRecord(String roomid, String AgencyID, final OnPlayRecordListener onPlayRecordListener) {

//        Version:字符串,接口的版本号,与本文档版本一致。
//        UA:字符串,
//                客户端类型。 iOS/Android/Windows/MacOS。
//        RoomID:字符串,教室 ID。VerificationKey:密文。机构+时间字符串(例如: ”rQ2ym
//        3Jfk3K+2016-11-01 08:30:00” )。字符串使用 s-1 算法加密。


        VerificationKeyUtils keyUtils = new VerificationKeyUtils();
        String VerificationKey = keyUtils.getVerificationKey(AgencyID);
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.addBodyParameter("UA", "Android");
        params.addBodyParameter("Version", Info.APIVERSION);
        params.addBodyParameter("RoomID", roomid);
        params.addBodyParameter("VerificationKey", VerificationKey);


        httpUtils.send(HttpRequest.HttpMethod.POST, Constant.getPlayRecordUrl(), params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                LogUtil.i("MVP","录像  "+responseInfo.result.toString());

                try {
                    JSONObject jsonObject = new JSONObject(responseInfo.result);

                    if (jsonObject.has("Result")){//数据返回成功

                        String result = jsonObject.getString("Result");

                        if("true".equals(result)){//数据返回正确

                            if(jsonObject.has("RecordUrl")){

                                String recordUrl = jsonObject.getString("RecordUrl");
                                onPlayRecordListener.playRecordSuc(recordUrl);
                            }

                        }

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    onPlayRecordListener.playRecordFal();
                }

            }

            @Override
            public void onFailure(HttpException e, String s) {
                LogUtil.i("MVP","录像  失败");
                onPlayRecordListener.playRecordFal();
            }
        });


    }

    @Override
    public void getRecordRoomId(String roomExID, String agencyID, final OnGetRoomExIDListener onGetRoomExIDListener)
    {
        VerificationKeyUtils keyUtils = new VerificationKeyUtils();
        String VerificationKey = keyUtils.getVerificationKey(agencyID);
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.addBodyParameter("RoomExID", roomExID);
        params.addBodyParameter("VerificationKey", VerificationKey);


        httpUtils.send(HttpRequest.HttpMethod.POST, Constant.getRoomID(), params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                LogUtil.i("GetRoomID","RoomID:"+responseInfo.result.toString());

                try {
                    JSONObject jsonObject = new JSONObject(responseInfo.result);

                    if (jsonObject.has("Result")) {//数据返回成功
                        if ("true".equals(jsonObject.getString("Result"))) {//数据返回正确
                            if (jsonObject.has("RoomID")) {
                                String roomID = jsonObject.getString("RoomID");
                                onGetRoomExIDListener.onSuccess(roomID);
                            }
                        }
                    } else {
                        if(jsonObject.has("code")){
                            onGetRoomExIDListener.onFailure(jsonObject.getString("code"));
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    onGetRoomExIDListener.onFailure(e.getMessage());
                }

            }

            @Override
            public void onFailure(HttpException e, String s) {
                LogUtil.i("GetRoomID", s);
                onGetRoomExIDListener.onFailure(s);
            }
        });
    }
}
